<template>
	<view class="bodyClass">
		<view class="itemClass">
			<view style="font-size: 18px;margin: 20px;">账号登录</view>
			<uni-forms ref="valiForm" :modelValue="baseFormData" :rules="rules">
				<uni-forms-item label="账号" required name="username">
					<uni-easyinput v-model="baseFormData.username" placeholder="请输入账号" />
				</uni-forms-item>
				<uni-forms-item label="密码" required name="password">
					<uni-easyinput v-model="baseFormData.password" placeholder="请输入密码" type="password"/>
				</uni-forms-item>
			</uni-forms>
			<button type="primary" @click="submit()">登录</button>
			<view style="font-size: 13px;margin: 20px;color:blue;" @click="zhuce">没有账号？前往注册</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				baseFormData:{},
				rules: {
					username: {
						rules: [{
							required: true,
							errorMessage: '账号不能为空'
						}]
					},
					password: {
						rules: [{
							required: true,
							errorMessage: '密码不能为空'
						}]
					}
				},
				httpUrl: 'http://192.168.31.178:8084/api',
			}
		},
		methods: {
			submit() {
				var that = this
				this.$refs.valiForm.validate().then(res => {
					console.log('success', res);
					uni.getLocation({
						type: 'gcj02',
						success: function (res) {
							that.baseFormData.latlon = res.latitude + ',' + res.longitude
							console.log('位置名称：' + res.name);
							console.log('详细地址：' + res.address);
							console.log('纬度：' + res.latitude);
							console.log('经度：' + res.longitude);
						}
					});
					uni.request({
						url: that.httpUrl + '/login/accountLogin',
						method: 'POST',
						data: that.baseFormData,
						success(res) {
							console.log('success11', res);
							if (res.data.code !== 200) {
								uni.showToast({title:res.data.msg,icon:'error',duration: 2000})
								return
							} else{
								uni.showToast({title:'登录成功',duration: 2000})
								uni.setStorageSync('token', res.data.data.token)
								uni.setStorageSync('username', res.data.data.username)
								uni.setStorageSync('isRole', res.data.data.isRole)
								uni.setStorageSync('id', res.data.data.id)
								uni.switchTab({
									url: '/pages/tabBar/order/order'
								});
							}
						},
						fail(err) {
							console.log('err:', err);
							uni.showToast({title:err.msg,icon:'error',duration: 2000})
						}
					})
				}).catch(err => {
					console.log('err', err);
				})
			},
			zhuce(){
				uni.navigateTo({
					url: '/pages/reg/reg'
				});
			},
		}
	}
</script>

<style>
.bodyClass{
	margin-top: 50px;
	text-align: center;
	display: flex;
	justify-content: center;
	align-items: center;
}
.itemClass{
	padding: 30px;
	width: 80%;
	background-color: aliceblue;
}
</style>
